package com.zsxj.xixi.common.utils;

import java.text.Collator;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

/**
 * @Description 类描述
 * @Author zhengbaole
 * @Date 2021/5/24
 **/
public final class SortUtils {

    /**
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2021/5/24 3:24 PM
     * @editTime 2021/5/24 3:24 PM
     */
    private SortUtils() { }

    /**
     * 给 list 按某个字段的中文拼音首字母顺序 排序
     *
     * @author zhengbaole
     * @lastEditor zhengbaole
     * @createTime 2021/5/24 3:22 PM
     * @editTime 2021/5/24 3:22 PM
     */
    public static <T, R> void sortByPinyin(List<T> collection, Function<T, R> mapper) {

        collection.sort((o1, o2) -> Collator.getInstance(Locale.CHINESE).compare(mapper.apply(o1), mapper.apply(o2)));
    }
}
